/**
 * @(#)Marking.java
 *
 *
 * @author 
 * @version 1.00 2009/5/8
 */

import java.io.*;
import java.lang.*;
import java.util.*;

public class Marking{
        
    /**
     * Creates a new instance of <code>Marking</code>.
     */ 
    public Marking(){
    			
    }
    
    /**
     * @param args the command line arguments
     */
    //public static void main(String[] args) {
	public boolean BaseMethod(String scheme,HandleInput inUser){
	//public void BaseMethod(String[] args) {
        // TODO code application logic here
       
        
        //read the marking scheme
        File myfile = new File(scheme);

		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
		
		BufferedReader reader = null;
        String fileContent = null;
        String comments=null;
                
		try{
			
			reader = new BufferedReader(new FileReader(myfile));
			String [] contents=null; //file content details	
			exportMark markAssignment= new exportMark();
        	
		      //read file content
		    	while ((fileContent = reader.readLine()) != null)
            	{
            		contents=fileContent.split("\\,");
            		System.out.println(contents[0]+", Maximum marks: "+contents[1]);
            		System.out.print("Mark?: ");
            		
            		String marks=inFromUser.readLine(); //user inputs marks
      				
      				while(Integer.parseInt(marks)>Integer.parseInt(contents[1]))
      				{
      					System.out.println("Invalid. Maximum marks is: "+contents[1]);
      					System.out.print("Mark?: ");
      					marks=inFromUser.readLine();
      				}
					
					markAssignment.setMarkingData(marks);
      				System.out.print("Enter comments(y/n)?: ");
      				comments=inFromUser.readLine();
					
					markAssignment.setMarkingData(comments);      				      				
      				if (comments.equalsIgnoreCase("y")){
      					System.out.println("Type your comment and press enter and press 'n' and Enter to end ");	
      				}
      				
      				while(!comments.equalsIgnoreCase("n"))
      				{
      					comments=inFromUser.readLine();
      					markAssignment.setMarkingData(comments);
      				}
      				
            	}//end while reading file contents
            	
      			markAssignment.writeMark(inUser.getSubjectID(),inUser.getAssignmentID(),inUser.getStudentID());	
      			return true;
		    } catch (FileNotFoundException e) {
		    	  System.out.println("File is not found.");
                  return false;
		    } catch (IOException e) {
		      	//e.printStackTrace();
                System.out.println("Invalid Arguments.");
                return false;
		    } catch (Exception e) {
		      	//e.printStackTrace();
                System.out.println("Invalid Arguments.");
                return false;
		    }
    }//end main
}

class exportMark{
	
	private List<String> markingData =new ArrayList<String>();
	
	public void writeMark(String subID, String assID, String stuID){
			
		File markingFolder = new File("C:\\java proj\\db\\"+subID+"\\marking\\");
		markingFolder.mkdir();
	
		String filename="C:\\java proj\\db\\"+subID+"\\marking\\"+stuID+" "+"Agile"+" "+assID+".txt";
		
		
		try{
			//write data
			FileWriter writer = new FileWriter(filename, true);
                writer.write("Subject   :" + subID);
                   writer.write(System.getProperty("line.separator"));
                   writer.write("Assignment  :" + assID);
                   writer.write(System.getProperty("line.separator"));
                   writer.write("Student ID  :" + stuID);
                   writer.write(System.getProperty("line.separator"));
                   writer.write(System.getProperty("line.separator"));
			for(int i=0;i<markingData.toArray(new String[markingData.size()]).length;i++)
		    {


		           if(!(markingData.toArray(new String[markingData.size()])[i]).equalsIgnoreCase("n")
		           			&& !(markingData.toArray(new String[markingData.size()])[i]).equalsIgnoreCase("y"))
		           {
		           		writer.write(markingData.toArray(new String[markingData.size()])[i]+",");		           	
		           }
		           else if(!(markingData.toArray(new String[markingData.size()])[i]).equalsIgnoreCase(""))
		           		writer.write(System.getProperty("line.separator"));
		           
          	}
          	
          	writer.flush();
		    writer.close();
          	System.out.println("Marking done!");
			
		}catch(IOException e){
			System.out.println(e);
		}
	}//end writeMark
	
	public void setMarkingData(String data){
		if (!data.trim().equalsIgnoreCase(""))
			markingData.add(data);
	}
}


//base on the input assignmentid and subjectId, choose the corresponse marking scheme
class MarkingScheme{
	public String getMarkingScheme(String subID, String AssID){
			
		File myfile = new File("c:\\java proj\\db\\"+subID);
		FileInputStream fis =null;
		
		if (myfile.list()==null)
		{
			System.out.println("Subject is not found.");

         }
									 					 		
			// Here we also get the list of file in the directory but
			// return it just as an array of String.
			String [] xfiles = myfile.list();
			 for (int i=0;i< xfiles.length;i++)	     
			 {	
			 	String filename = xfiles[i];
			 	filename=filename.substring(0,filename.length()-4); //get rid of file extension
			 	if (filename.equalsIgnoreCase(AssID))
			 	{
			 		return "C:\\java proj\\db\\"+subID+"\\"+filename+".txt";
			 		//System.out.println(filename);
			 	}
			 	else
			 	{
			 		System.out.println("not found assignmentID");	
			 	}
			 	
			 }//end for
		return null;
	}
}